
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        dom = ecui.dom;

    Object.assign(
        NS.data,
        {
            permission: {
                venue: {
                    'delete': ['preOrderDelete', 'fixedOrderDelete', 'roadShowOrderDelete'],
                    'audit': ['preOrderAuditFinal', 'fixedOrderAuditFirst', 'fixedOrderAuditFinal', 'roadShowOrderAuditFirst', 'roadShowOrderAuditFinal'],
                    'auditFirst': ['fixedOrderAuditFirst', 'roadShowOrderAuditFirst'],
                    'auditFinal': ['preOrderAuditFinal', 'fixedOrderAuditFinal', 'roadShowOrderAuditFinal'],
                    'modify': ['preOrderModify', 'fixedOrderModify', 'roadShowOrderModify']
                },
                contract: {
                    'delete': ['courtDelete', 'lottingDelete', 'commerceDelete', 'selfDelete'],
                    'audit': ['courtAuditFirst', 'courtAuditFinal', 'lottingAuditFirst', 'lottingAuditFinal', 'commerceAuditFirst', 'commerceAuditFinal', 'selfAuditFirst', 'selfAuditFinal'],
                    'auditFirst': ['courtAuditFirst', 'lottingAuditFirst', 'commerceAuditFirst', 'selfAuditFirst'],
                    'auditFinal': ['courtAuditFinal', 'lottingAuditFinal', 'commerceAuditFinal', 'selfAuditFinal'],
                    'modify': ['courtModify', 'lottingModify', 'commerceModify', 'selfModify']
                },
                paly: {
                    'delete': ['playDelete'],
                    'audit': ['playAuditFinal'],
                    'modify': ['playModify']
                },
                sponsor: {
                    'delete': ['sponsorListDelete'],
                    'audit': ['sponsorListAuditFinal'],
                    'modify': ['sponsorListModify']
                },
                musician: {
                    'delete': ['musicianListDelete'],
                    'audit': ['musicianListAuditFinal'],
                    'modify': ['musicianListModify']
                }
            }
        }
    );
    mao.joinPermission = function (module, type) {
        var moduleMap = ['', 'venue', 'contract', 'paly', 'sponsor', 'musician'];
        return NS.data.permission[moduleMap[module]][type].map(function (item) { return moduleMap[module] + ':' + item; }).join('||');
    };
    Object.assign(
        NS.ui,
        {
            ContractCommentSave: core.inherits(
                mao.ui.CommentSave,
                {
                    Operate: ecui.inherits(
                        ecui.ui.Control,
                        function (el, options) {
                            ecui.ui.Control.call(this, el, options);
                            this._sAction = options.action;
                        },
                        {
                            onclick: function (event) {
                                var parent = this.getParent(),
                                    value = parent.getValue();
                                parent.action = this._sAction;
                                var elements = parent.getInput().form.elements;
                                if (elements.action) {
                                    elements.action.value = this._sAction;
                                } else {
                                    elements.auditStatus.value = this._sAction;
                                }
                                if (this._sAction !== '7') {
                                    event.exit();
                                    ecui.dispatchEvent(this.getParent(), 'submit');
                                }
                            }
                        }
                    ),
                    onsubmit: function () {
                        if (this._oRequest) {
                            return;
                        }
                        this._oRequest = true;
                        var form = this.getInput().form;
                        if (this.action === '7') {
                            // 评论
                            ecui.esr.request(
                                'data@JSON /erp-management/contract/comment?id=' + form.id.value + '&comment=' + form.actionRemark.value,
                                function () {
                                    this._oRequest = false;
                                    var data = ecui.esr.getData('data');
                                    if (data instanceof Object) {
                                        ecui.tip('warn', '评论成功');
                                        var route = ecui.esr.findRoute(this.getMain());
                                        delete route.CACHE;
                                        ecui.esr.reload();
                                    }
                                }.bind(this),
                                function () {
                                    this._oRequest = false;
                                }.bind(this)
                            );
                        } else if (this.action === '6') {
                            // 删除
                            ecui.esr.request(
                                'data@FORM /erp-management/contract/delete?' + form.name,
                                function () {
                                    this._oRequest = false;
                                    var data = ecui.esr.getData('data');
                                    if (data instanceof Object) {
                                        ecui.tip('warn', '删除成功');
                                        ecui.esr.getRoute('/approval/initiate.initiate').CACHE = false;
                                        history.go(-1);
                                    }
                                }.bind(this),
                                function () {
                                    this._oRequest = false;
                                }.bind(this)
                            );
                        } else if (this.action === '5') {
                            // 编辑
                        } else {
                            // 0-撤销 1-提交 2-驳回 3-通过 4-中止
                            var tip = ['撤销', '提交', '驳回', '通过', '中止'];
                            ecui.esr.request(
                                'data@FORM /erp-management/contract/action?' + form.name,
                                function () {
                                    this._oRequest = false;
                                    var data = ecui.esr.getData('data');
                                    if (data instanceof Object) {
                                        ecui.tip('warn', tip[+this.action] + '成功');
                                        if (ecui.esr.getData('initiate') === 'true') {
                                            ecui.esr.getRoute('/approval/initiate.initiate').CACHE = false;
                                        } else {
                                            ecui.esr.getRoute('/approval/approve').CACHE = false;
                                        }
                                        history.go(-1);
                                    }
                                }.bind(this),
                                function () {
                                    this._oRequest = false;
                                }.bind(this)
                            );

                        }
                    }
                }
            )
        }
    );
    ecui.esr.addRoute('ticket', {
        model: ['detail@GET /erp-management/contract/${id}'],
        weight: 4,
        frame: true,
        onbeforerequest: function (context) {
        },
        onbeforerender: function (context) {
        },
        onafterrender: function (context) {
        }
    });
}());

